package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.ImagePickerAdapter;
import com.dlwx.firefighting.utils.KeyBoardUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.widget.SelectDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单评价界面
 */
public class OrderEvaluateActivity extends BaseActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener {
    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.evaluate_star_one)
    ImageView starOne;
    @BindView(R.id.evaluate_star_two)
    ImageView starTwo;
    @BindView(R.id.evaluate_star_three)
    ImageView starThree;
    @BindView(R.id.evaluate_star_four)
    ImageView starFour;
    @BindView(R.id.evaluate_star_five)
    ImageView starFive;
    @BindView(R.id.tv_score_level)
    TextView tv_commen;
    @BindView(R.id.edit_evaluate)
    EditText editEvaluate;

    public static final int IMAGE_ITEM_ADD = -1;
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_PREVIEW = 101;
    private ImagePickerAdapter adapter;
    private ArrayList<ImageItem> selImageList; //当前选择的所有图片
    private int maxImgCount = 4;               //允许选择图片最大数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_evaluate);
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initView();
        initRecy();
    }

    private void initRecy() {
        selImageList = new ArrayList<>();
        adapter = new ImagePickerAdapter(this, selImageList, maxImgCount);
        adapter.setOnItemClickListener(this);

        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("我的评价");
        titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("发表");

        /**edittext默认不显示光标，触摸显示光标*/
        editEvaluate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    editEvaluate.setCursorVisible(true);
                    KeyBoardUtils.openKeybord(editEvaluate, OrderEvaluateActivity.this);
                    return false;
                }
                return true;
            }
        });
        /**监听edittext换行*/
        editEvaluate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //点击换行时，如果已经5行了，就使edittext禁用换行
                int lineCount = editEvaluate.getLineCount();
                if (lineCount >= 5) {
                    editEvaluate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
                        }
                    });
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }


    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.evaluate_star_one,
            R.id.evaluate_star_two, R.id.evaluate_star_three, R.id.evaluate_star_four,
            R.id.evaluate_star_five})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.title_right_one_tv:
                ToastUtils.toastForShort(this, "发表");
                break;
            case R.id.evaluate_star_one:
                lightStar(1);
                break;
            case R.id.evaluate_star_two:
                lightStar(2);
                break;
            case R.id.evaluate_star_three:
                lightStar(3);
                break;
            case R.id.evaluate_star_four:
                lightStar(4);
                break;
            case R.id.evaluate_star_five:
                lightStar(5);
                break;
        }
    }

    /**
     * 点亮star数量
     */
    private void lightStar(int number) {
        switch (number) {
            case 1:
                starOne.setImageResource(R.mipmap.icon_star_select);
                starTwo.setImageResource(R.mipmap.icon_star_unselect);
                starThree.setImageResource(R.mipmap.icon_star_unselect);
                starFour.setImageResource(R.mipmap.icon_star_unselect);
                starFive.setImageResource(R.mipmap.icon_star_unselect);
                tv_commen.setText("非常差");
                break;
            case 2:
                starOne.setImageResource(R.mipmap.icon_star_select);
                starTwo.setImageResource(R.mipmap.icon_star_select);
                starThree.setImageResource(R.mipmap.icon_star_unselect);
                starFour.setImageResource(R.mipmap.icon_star_unselect);
                starFive.setImageResource(R.mipmap.icon_star_unselect);
                tv_commen.setText("较差");
                break;
            case 3:
                starOne.setImageResource(R.mipmap.icon_star_select);
                starTwo.setImageResource(R.mipmap.icon_star_select);
                starThree.setImageResource(R.mipmap.icon_star_select);
                starFour.setImageResource(R.mipmap.icon_star_unselect);
                starFive.setImageResource(R.mipmap.icon_star_unselect);
                tv_commen.setText("一般");
                break;
            case 4:
                starOne.setImageResource(R.mipmap.icon_star_select);
                starTwo.setImageResource(R.mipmap.icon_star_select);
                starThree.setImageResource(R.mipmap.icon_star_select);
                starFour.setImageResource(R.mipmap.icon_star_select);
                starFive.setImageResource(R.mipmap.icon_star_unselect);
                tv_commen.setText("较好");
                break;
            case 5:
                starOne.setImageResource(R.mipmap.icon_star_select);
                starTwo.setImageResource(R.mipmap.icon_star_select);
                starThree.setImageResource(R.mipmap.icon_star_select);
                starFour.setImageResource(R.mipmap.icon_star_select);
                starFive.setImageResource(R.mipmap.icon_star_select);
                tv_commen.setText("非常好");
                break;
            default:
                starOne.setImageResource(R.mipmap.icon_star_unselect);
                starTwo.setImageResource(R.mipmap.icon_star_unselect);
                starThree.setImageResource(R.mipmap.icon_star_unselect);
                starFour.setImageResource(R.mipmap.icon_star_unselect);
                starFive.setImageResource(R.mipmap.icon_star_unselect);
                break;
        }
    }

    /**
     * 底部弹出选择拍照还是图库
     */
    private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) {
        SelectDialog dialog = new SelectDialog(this, R.style.transparentFrameWindowStyle,
                listener, names);
        if (!this.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case IMAGE_ITEM_ADD:
                List<String> names = new ArrayList<>();
                names.add("拍照");
                names.add("相册");
                showDialog(new SelectDialog.SelectDialogListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0: // 直接调起相机
                                /**
                                 * 0.4.7 目前直接调起相机不支持裁剪，如果开启裁剪后不会返回图片，请注意，后续版本会解决
                                 *
                                 * 但是当前直接依赖的版本已经解决，考虑到版本改动很少，所以这次没有上传到远程仓库
                                 *
                                 * 如果实在有所需要，请直接下载源码引用。
                                 */
                                //打开选择,本次允许选择的数量
                                ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());
                                Intent intent = new Intent(OrderEvaluateActivity.this, ImageGridActivity.class);
                                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                                startActivityForResult(intent, REQUEST_CODE_SELECT);
                                break;
                            case 1:
                                //打开选择,本次允许选择的数量
                                ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());
                                Intent intent1 = new Intent(OrderEvaluateActivity.this, ImageGridActivity.class);
                                /* 如果需要进入选择的时候显示已经选中的图片，
                                 * 详情请查看ImagePickerActivity
                                 * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
                                startActivityForResult(intent1, REQUEST_CODE_SELECT);
                                break;
                            default:
                                break;
                        }

                    }
                }, names);


                break;
            default:
                //打开预览
                Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
                intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) adapter.getImages());
                intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);
                startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                break;
        }
    }

    ArrayList<ImageItem> images = null;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
                    selImageList.addAll(images);
                    adapter.setImages(selImageList);
                }
            }
        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                if (images != null) {
                    selImageList.clear();
                    selImageList.addAll(images);
                    adapter.setImages(selImageList);
                }
            }
        }
    }

}
